$(function(){
    let index = 0;
    let mianobj = {
        shopid :0,
        areaid :0
    }
    show1();
    clickA();
    clickB();
    function clickA(){
        $("#segmentedControl").on("tap","a",function () {
            index = $(this).index()
            if ($(".nav_top1").eq($(this).index()).css('display') == 'block') {
                $(".nav_top1").eq($(this).index()).hide();
                return false;
            }
                $(".nav_top").hide().eq($(this).index()).show();
            
        });
    }
    function clickB(){
        $(".nav_top1").on("tap","li",function(){
            if(index == 0){
                mianobj.shopid = $(this).data('cid');
            }else{
                mianobj.areaid = $(this).data('areaid');
            }
            show2(mianobj.shopid,mianobj.areaid);
            //让二级菜单隐藏
            if($(this).parent().css("display") == 'block'){
                $(this).parent().hide(500);
            }
        })
    }
    function show1(){
        $.get("http://193.112.55.79:9090/api/getgsshop",function(result){
            let data = result.result;
            let html = "";
            data.forEach(element => {
                html += 
                `
                <li data-cid="${element.shopId}" class="mui-table-view-cell">${element.shopName}</li>
                `
            });
            $("#nav_top1").html(html);
        });
        $.get("http://193.112.55.79:9090/api/getgsshoparea",function(result){
            let data = result.result;
            let html = "";
            data.forEach(element=>{
                html +=
                `
                <li data-areaId="${element.areaId}" class="mui-table-view-cell">${element.areaName}</li>
                `
            });
            $("#nav_top2").html(html);
        });
        show2();
    };
    function show2(shopid,areaid){
        $.get("http://193.112.55.79:9090/api/getgsproduct",{
            shopid : mianobj.shopid,
            areaid : mianobj.areaid,
        },function(result){
            let data = result.result;
            let html = template("mianTpl",{arr:data});
            $(".mmm_mian").html(html);
        })
    }
})